<template>
    <c-dialog v-model:show="dialogFlag" v-if="dialogFlag" :title="title" @confirm="save" @close="close" :width="500">
        <el-tree v-if="initover" ref="treeRef" :props="options" node-key="id" :data="lists" show-checkbox @check-change="handleCheckChange" :default-expanded-keys="defaultExpandedKeys" :default-checked-keys="defaultCheckedList"/>
    </c-dialog>
</template>
<script setup>
import { ref,onMounted,reactive } from 'vue';
import {getAuthList,saveAuth,getRoleAuthList} from '@/api/system'
import cDialog from '@/components/custom/cDialog.vue'
import { message } from '@/utils/element'
import { ElMessageBox } from 'element-plus'

const props = defineProps({
    disabled:'',
    show: {
        type: Boolean,
        default: false
    },
    title: String,
    editData: Object,
    type:Number // 操作类型：1为新增，2为编辑，3为查看
})
const emit = defineEmits(['update:show','success'])

const options = ref({
  id: 'id',
  label: 'name',
  children: 'children',
  disabled: 'disabled' // 禁用标志
})
const lists = ref([])
const defaultExpandedKeys = ref([])
const treeRef = ref(null)
const defaultCheckedList = ref([])//ref([35, 36, 37, 38])  // 需要默认选择的模块
const selectedKeys = ref([]) // 当前选择的值
const initover = ref(false)

const dialogFlag = ref(props.show)

onMounted(()=>{
    getAuthList({
        id:99,
        roleId:props.editData.id
    }).then(res =>{
        lists.value = res.children
        //设置默认展开
        lists.value.forEach(item => {
            defaultExpandedKeys.value.push(item.id)
        });

        // 获取该角色的权限列表
        getRoleAuthList({roleId:props.editData.id}).then(res2 =>{
            if(res2.code ==0 || res2.success){
                const authlists = res2.data[props.editData.roleCode] || []
                authlists.forEach(code =>{
                    let id = findEleToArr(code,lists.value)
                    if(id){
                        defaultCheckedList.value.push(id)
                    }
                })
            }else{
                message(res2.message || '无法连接网络，请检查您的网络连接',3)
            }
            initover.value = true
        })
    })
})


// 判断ele是否在arr中
let id = ''
const findEleToArr=(ele,arr)=>{
    for(let i=0;i<arr.length;i++){
        let item = arr[i]
        if(item.permission == ele){
            id = item.id
            break;
        }else if(item.children){
            findEleToArr(ele,item.children)
        }
    }
    return id
}

const save = async() => {
    if(selectedKeys.value.length<1){
        return message('请选择权限菜单',3)
    }
    let params = []
    selectedKeys.value.forEach(id =>{
        params.push({resourceId:id,roleId:props.editData.id})
    })
    //console.log("params:",params)
    const res = await saveAuth(params)
    if(res.code ==0 || res.success){
        message('设置成功',2)
        emit('success')
        close()
    }else{
        message(res.message,3)
    }
}

const handleCheckChange =(data)=>{
    selectedKeys.value = treeRef.value.getCheckedKeys(false)
    //console.log("选择：",selectedKeys.value)
}


const close = () => {
    emit('update:show', false)
}
</script>
<style lang="scss" scoped>
    
</style>
